package com.general.framework.core.lang;

import com.general.framework.core.exception.Ex;

import java.util.Collection;
import java.util.Map;

/**
 * 验证工具
 * @author leejean
 */
public abstract class Validate {

    /**
     * 如果不为空
     */
    public static void isNotNull(final Object object, final String message, final Object... params) {
        if (object != null) {
            throw Ex.violation(message, params);
        }
    }

    /**
     * 如果为空
     */
    public static void isNull(final Object object, final String message, final Object... params) {
        if (object == null) {
            throw Ex.violation(message, params);
        }
    }

    /**
     * 如果为空或者为0
     */
    public static void isNullOrZero(final Number number, final String message, final Object... params) {
        if (number == null || number.equals(0)) {
            throw Ex.violation(message, params);
        }
    }

    /**
     * 断言String对象不为空
     */
    public static void isBlank(final String object, final String message, final Object... params) {
        if (Strings.isBlank(object)) {
            throw Ex.violation(message, params);
        }
    }


    /**
     * 断言集合对象不为空
     */
    public static void isEmpty(final Collection object, final String message, final String... params) {
        if (object == null || object.isEmpty()) {
            throw Ex.violation(message, params);
        }
    }

    /**
     * 断言Map对象不为空
     */
    public static void isEmpty(final Map object, final String message, final String... params) {
        if (object == null || object.isEmpty()) {
            throw Ex.violation(message, params);
        }
    }

    /**
     * 断言数组对象不为空
     */
    public static void isEmpty(final Object[] array, final String message, final String... params) {
        if (array == null || array.length == 0) {
            throw Ex.violation(message, params);
        }
    }

    /**
     * 断言字符串不为空
     */
    public static void isEmpty(final String str, final String message, final String... params) {
        if (Strings.isEmpty(str)) {
            throw Ex.violation(message, params);
        }
    }

    /**
     * 断言某字符串为手机格式
     */
    public static void isMobile(final String object, final String message, final String... params) {
        if (!Strings.isMobile(object)) {
            throw Ex.violation(message, params);
        }
    }

    /**
     * 密码校验
     */
    public static void password(String password, final String message, final Strings... params) {
        final String regex = "[a-zA-Z0-9.!@#$%^&*_-]{6,18}";
        boolean b = password.matches(regex);
        if (!b) {
            throw Ex.violation(message, params);
        }
    }

    /**
     * url检验
     */
    public static void isUrl(String url, final String message, final Strings... params) {
        if (!Strings.isUrl(url)) {
            throw Ex.violation(message, params);
        }
    }

    public static void idCard(String idCardNum, final String message, final Strings... params) {
        if (!Strings.isIdCardNum(idCardNum)) {
            throw Ex.violation(message, params);
        }
    }


    public static void isFalse(final Boolean bool, final String message, final Object... params) {
        if (!bool) {
            throw Ex.violation(message, params);
        }
    }

    public static void isTrue(final Boolean bool, final String message, final Object... params) {
        if (bool) {
            throw Ex.violation(message, params);
        }
    }

}
